package com.yanqu.road.server.manager.activity.apprentice.handler.impl;

import com.yanqu.road.entity.activity.apprentice.ApprenticeEffectBuffProject;
import com.yanqu.road.entity.activity.apprentice.config.ApprenticeConfig;
import com.yanqu.road.entity.activity.apprentice.config.ApprenticeProjectConfig;
import com.yanqu.road.entity.activity.apprentice.data.ApprenticeBuffProjectData;
import com.yanqu.road.entity.activity.apprentice.enums.ApprenticeProjectSortEnum;
import com.yanqu.road.entity.enums.eOccupationType;
import com.yanqu.road.server.manager.activity.apprentice.CrossApprenticeActivity;
import com.yanqu.road.server.manager.activity.apprentice.handler.ApprenticeProjectHandler;
import com.yanqu.road.server.manager.activity.apprentice.handler.param.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ApprenticeDoProjectType6Handler implements ApprenticeProjectHandler {

    /**
     * 执行项目
     * @param req
     * @return
     */
    @Override
    public ApprenticeDoProjectResp doProject(ApprenticeDoProjectReq req) {
        return null;
    }

    /**
     * 接下来{0}次行程，|每多选择一个同职业项目，当次天资{1}。
     * @param req
     */
    @Override
    public void countTalentTypeAddRate(ApprenticeCountTalentAddRateReq req) {
        CrossApprenticeActivity activity = req.getActivity();
        ApprenticeConfig config = activity.getConfig();
        ApprenticeProjectConfig projectConfig = req.getProjectConfig();
        Map<Integer, Integer> resultMap = req.getResultMap();
        ApprenticeBuffProjectData buffProjectData = req.getBuffProjectData();
        //buff次数减一
        buffProjectData.addNum(-1);
        int addRate = 0;
        //单个相同加成rate
        int oneSameAddRate = Integer.parseInt(projectConfig.getParam()) - 1000;
        //职业相同数量MAP
        Map<Integer, Integer> talentSameMap = new HashMap<>();
        for (int selectProjectId : req.getSelectProjectIds()) {
            ApprenticeProjectConfig selectProjectConfig = config.getProjectConfig(selectProjectId);
            if (selectProjectConfig == null) {
                continue;
            }
            if (selectProjectConfig.getSort() == ApprenticeProjectSortEnum.SORT_TYPE_0.getType()) {
                continue;
            }
            int talentType = Integer.parseInt(selectProjectConfig.getParam());
            int oldNum = talentSameMap.getOrDefault(talentType, 0);
            talentSameMap.put(talentType, oldNum + 1);
        }
        //遍历【职业相同数量MAP】计算出加成
        for (Map.Entry<Integer, Integer> entry : talentSameMap.entrySet()) {
            int num = entry.getValue();
            if (num <= 1) {
                continue;
            }
            addRate += oneSameAddRate * (num - 1);
        }
        //注入加成值到MAP
        for (eOccupationType type : eOccupationType.values()) {
            int talentType = type.getValue();
            int oldRate = resultMap.getOrDefault(talentType, 0);
            resultMap.put(talentType, oldRate + addRate);
        }
        //生效buff结果结果集注入
        List<ApprenticeEffectBuffProject> effectBuffProjects = req.getEffectBuffProjects();
        ApprenticeEffectBuffProject effectBuffProject = new ApprenticeEffectBuffProject(buffProjectData.getProjectId(), addRate);
        effectBuffProjects.add(effectBuffProject);
    }

    @Override
    public void countSortTypeAddRate(ApprenticeCountSortAddRateReq req) {

    }
}
